﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simulator
{
    class Manager
    {
        private Node[] nodes;
        static Manager manager;
        private Manager()
        {
            
        }
        public static Manager getManager()
        {
            if (manager == null)
                manager = new Manager();
            return manager;
        }

        public void InitialClustering()
        {
            foreach (Node node in nodes)
            {
                node.clusterize1();
            }
            foreach (Node node in nodes)
            {
                node.clusterize2();
            }
        }

        public void Broadcast(Message message)
        {
            foreach (Node node in nodes)
            {
                if (node.canHear(nodes[message.source]))
                {
					node.messageRecieved(message);
                }
            }
        }

        public void probe(int source, HashSet<int> nodesIHear, int? myClusterHead)
        {
            foreach (Node node in nodes)
            {
                if (node.canHear(nodes[source]))
                {
                    node.probed(source, nodesIHear, myClusterHead);
                }
            }
        }
    }

    static class extentions
    {
        public static double maxDistSqr;
        public static bool canHear(this Node node, Node otherNode)
        {
            return (node.X - otherNode.X)*(node.X - otherNode.X) + (node.Y - otherNode.Y)*(node.Y - otherNode.Y) <=
                   maxDistSqr;
        }
    }
}
